package cn.yogehaoren.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author WangNing
 * @since 1.0
 */
public class TopicProducer {

    public static void main(String[] args) throws IOException, TimeoutException {

        //1. 创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("k8s-node2");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");

        //2. 通过连接工厂创建连接
        Connection connection = connectionFactory.newConnection();

        // 3. 通过Connection创建Channel
        Channel channel = connection.createChannel();

        // 4. 声明
        String exchangeName = "test_topic_exchange";
        String routingKey1 = "user.save";
        String routingKey2 = "user.update";
        String routingKey3 = "user.delete.abc";

        for (int i = 0; i < 100; i++) {
            String msg = "Hello World RabbitMQ 4 Topic Exchange Message...";
            channel.basicPublish(exchangeName, routingKey1, null,msg.getBytes());
            channel.basicPublish(exchangeName, routingKey2, null,msg.getBytes());
            channel.basicPublish(exchangeName, routingKey3, null,msg.getBytes());
        }


        // 5. 关闭连接
        channel.close();
        connection.close();
    }


}
